package com.tanhua.server.service;

import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.TimeLose;
import com.tanhua.dubbo.api.PeachBlossomApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.PeachBlossom;
import com.tanhua.model.vo.PeachBlossomVo;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class PeachBlossomService {
    @DubboReference
    private UserApi userApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private PeachBlossomApi peachBlossomApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    /**
     * 桃花传音-发送语音
     * @param soundFile
     */
    public void send(MultipartFile soundFile) throws IOException {
        Long userId = UserHolder.getUserId();

        //上传阿里云，返回存储路径
        try {
            String voiceUrl = ossTemplate.upload(soundFile.getOriginalFilename(),soundFile.getInputStream());
            // 构建实体
            PeachBlossom peachBlossom = new PeachBlossom();
            peachBlossom.setSoundUrl(voiceUrl);
            peachBlossom.setCreated(System.currentTimeMillis());
            peachBlossom.setUserId(userId);
            // 调用api保存到数据库
            peachBlossomApi.send(peachBlossom);

            //保存当日接收次数
            String key = Constants.PB_USER_KEY + UserHolder.getUserId();
            redisTemplate.opsForValue().set(key, "10", Duration.ofDays(1));
        } catch (IOException e) {
            log.error("SoundService 层上传语音失败..." + e);
            e.printStackTrace();
        }

    }

    /**
     * 桃花传音-接收语音
     * @return
     */
    public PeachBlossomVo accept() {
        //1.从MongoDB中随机查询一个音频信息
        //long userId = UserHolder.getUserId();
        long userId = new Random().nextInt(10) + 1;
        PeachBlossom peachBlossom = peachBlossomApi.findVoice(userId);

        //2.调用Api查询用户信息
        UserInfo userInfo = userInfoApi.findById(userId);

        //3.从redis中获取今日剩余次数
        String key = Constants.PB_USER_KEY + UserHolder.getUserId();
        String count = redisTemplate.opsForValue().get(key);
        //要加入一个接受异性语音
        //4、封装返回数据,构造返回值
        PeachBlossomVo vo = new PeachBlossomVo();
        vo.setId(UserHolder.getUserId());
        vo.setAge(userInfo.getAge());
        vo.setAvatar(userInfo.getAvatar());
        vo.setGender(userInfo.getGender());
        vo.setNickname(userInfo.getNickname());
        vo.setSoundUrl(peachBlossom.getSoundUrl());
        vo.setRemainingTimes(Integer.parseInt(count));
        //将今日查询次数减一
        String numberAccept = Long.toString(redisTemplate.opsForValue().decrement(key));
        // 当前时间距离晚上0点的剩余秒数
        Long second = (TimeLose.getMiol() - System.currentTimeMillis()) / 1000;
        // TODO:  定时任务,每天晚上12点重置次数
        redisTemplate.opsForValue().set(key, numberAccept, second, TimeUnit.SECONDS);
        return vo;
    }


}
